<template>
  <div v-if="$store.state.user.onlineStatus">
    <div class="mask">
    </div>
    <div class="content">
        <div class="toggle-c" :class="showBack?'show-back':'show-front'">
          <div class="show-login" v-if="showBack">
            <div class="show-login-tile">
              用户登录
            </div>
            <div class="show-login-input">
              <div class="login-username">
                <img src="../../assets/beneform4j/login/login_icon_user_sel.png" alt="">
                <input type="text" disabled="true" v-model="userName">
              </div>
              <div class="login-pwd">
                <img src="../../assets/beneform4j/login/login_icon_key_sel.png" alt="">
                <input type="password" v-model="userPwd" placeholder="请输入密码">
              </div>
            </div>
            <div class="login-boottom-action">
              <el-button @click="login" style="width: 280px;height: 40px" type="primary">登录</el-button>
            </div>
            <div @click="gotoLogin" class="show-toggle-user color">
                切换用户
              </div>
          </div>
          <div v-if="!showBack">
            <div class="clearfix header-title">
              <div class="float-l">
                <img src="../../assets/beneform4j/home/overtime_icon_tlie.png" alt="">
                <span>温馨提示</span>
              </div>
              <div class="float-r" @click="hidden"><img src="../../assets/beneform4j/home/icon_close.png" alt=""></div>
            </div>
            <div class="body-img">
              <img src="../../assets/beneform4j/home/overtime_icon_404.png" alt="">
            </div>
            <div class="alert-info">
              登录会话超时，请重新登录
            </div>
            <div class="boottom-action">
              <el-button @click="reLogin">重新登录</el-button>
              <el-button @click="gotoLogin">切换用户</el-button>
            </div>
          </div>
        </div>
    </div>
  </div>
</template>

<script>

import { loginByUsername, getSessionUserApi } from '../api/login'
export default {
  data() {
    return {
      showBack: false,
      userPwd: '',
      userName: ''
    }
  },
  mounted: function() {
    // this.showBack = true
    getSessionUserApi().then(response => {
      /**
       * 获取用户信息
       */
      this.showBack = false
      if (response.success) {
        this.userName = response.data.userName
        this.$store.dispatch('SetUserInfo', response)
      }
    })
    // document.body.style.overflow = 'hidden'
  },
  destroyed() {
    document.body.style.overflow = 'initial'
  },
  methods: {
    login() {
      if (!this.userPwd) {
        this.$message({
          message: '请输入密码',
          type: 'error'
        })
        return
      }
      // 登录请求
      loginByUsername(this.userName, this.userPwd).then((response) => {
        if (response.success) {
          // 保存用户token, info
          this.$store.dispatch('LoginByUsername', response.data).then(() => {
            this.loading = false
          })
          document.body.style.overflow = 'initial'
          // 设置登录会话激活状态
          this.$store.dispatch('ToggleOnlineStatusNormal').then(() => {
            this.showBack = false
            this.userPwd = ''
            this.$message({
              message: '登录成功',
              type: 'success'
            })
          })
        }
      })
    },
    reLogin() {
      this.showBack = true
    },
    hidden() {
      this.$store.dispatch('ToggleOnlineStatusNormal')
    },
    gotoLogin() {
      this.$store.dispatch('LogOut').then(() => {
        this.$store.dispatch('ToggleOnlineStatusNormal')
        this.$router.push(
          {
            path: '/login',
          }
        )
      })
    }
  }
}
</script>

<style scoped>
/* @import '../style/animation.css'; */
.content{
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 110;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}
.mask{
  position: fixed;
  background: rgba(0, 0, 0, 0.5);
}
.toggle-c{
  width: 400px;
  height: 373px;
  margin: 0 auto;
  position: relative;
  background: #FFF;
  transition: 0.5s ease all;
}
.header-title{
  margin: 20px 16px;
  margin-bottom: 0;
  border-bottom: 1px dotted #e9e9e9;
  font-weight: bold;
  padding-bottom: 20px;
}
.body-img{
  margin: 20px 0;
}
.alert-info{
  padding-bottom: 20px;
  border-bottom: 1px dotted #e9e9e9;
  margin-left: 20px;
  margin-right: 20px;
}
.boottom-action{
  margin: 20px;
}
.header-title>.float-l>img{
  position: relative;
  top: 3px;
}

.show-back{
  transform: rotateY(180deg);
}
.show-front{
  transform: rotateY(0deg);
}
.show-back .show-login{
  transform: rotateY(180deg);
}
.show-front .show-login{
  transform: rotateY(0deg);
}
.show-login-tile{
  font-weight: bold;
  font-size: 18px;
  margin-top: 56px;
  text-align: center;
  margin-bottom: 20px;
}
.show-login-input {
  margin: 0 64px;
}
.show-login-input>div{
  position: relative;
}
.show-login-input input{
  -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 40px;
    line-height: 40px;
    outline: none;
    padding: 0 40px;
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    width: 100%;
    border-top: none;
    border-left: none;
    border-right: none;
}
.login-pwd{
  margin-top: 24px;
}
.login-boottom-action{
  margin-top: 40px;
}
.login-username>input{
  cursor: not-allowed;
}
.show-toggle-user{
  cursor: pointer;
  margin-top: 20px;
}
.show-login-input>div>img{
  position: absolute;
  left: 10px;
  top: 10px;
}
</style>
